package net.sx.minemodule.mvp.presenters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.Log;
import com.kedll.library.util.RxBus;
import com.kedll.library.util.ToastUtil;
import com.kedll.library.widget.MySwitchView;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.R;
import net.sx.minemodule.mvp.bean.PushValueBean;
import net.sx.minemodule.mvp.event.MsgPushEvent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MineMsgPushPresenter extends BasePresenter {
    private MsgAdapter adapter;

    public MsgAdapter getAdapter() {
        return adapter;
    }

    public void getMsgPushList(String msgPushJson, Context context) {
        List<PushValueBean> mBean = JSON.parseArray(msgPushJson, PushValueBean.class);
        if (mBean != null) {    //add shibo.zheng
            adapter = new MsgAdapter(mBean, context);
        }
    }

    public class MsgAdapter extends RecyclerView.Adapter {
        List<PushValueBean> list;
        Context context;

        public MsgAdapter(List<PushValueBean> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mine_item_msgpush, null);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(lp);
            return new MsgHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            MsgHolder holder = (MsgHolder) viewHolder;
            holder.textView.setText(list.get(i).getName());
            holder.mySwitchView.setCheck(list.get(i).getValue() == 1);
            holder.mySwitchView.setSwitchOpenColor(ContextCompat.getColor(context, R.color.color_a541c9));
            holder.position = i;
            holder.mySwitchView.setOnCheckListener((v, isCheck) -> {
                ToastUtil.showToast(i + "*----isCheck=" + isCheck);
                Map<String, Integer> map = new HashMap<>();
                for (int k = 0; k < list.size(); k++) {
                    PushValueBean b = list.get(k);
                    if (k == i) {
                        map.put(b.getType(), isCheck ? 1 : 0);
                    } else {
                        map.put(b.getType(), b.getValue());
                    }
                }
                Log.e("====",JSON.toJSONString(map));
                MineApiUtils.modifyUserInfo("PushSign", JSON.toJSONString(map), s -> {

//                    userService.setUserModel(s);
                    list.get(i).setValue(isCheck ? 1 : 0);
                    RxBus.getDefault().post(new MsgPushEvent(s));
                }, throwable -> {
                });
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }


        class MsgHolder extends RecyclerView.ViewHolder {
            public TextView textView;
            public MySwitchView mySwitchView;
            public int position;

            public MsgHolder(@NonNull View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_left_content_entry);
                mySwitchView = itemView.findViewById(R.id.my_switch_view);
            }
        }

    }
}
